﻿<link href="~/lib/layui/css/layui.css" rel="stylesheet" />  
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/layui/layui.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<h2>登录</h2>
 
<table class="layui-table">
    <tr>
        <td>用户名</td>
        <td><input type="type" id="name" value="" /></td>
    </tr>

    <tr>
        <td>密码</td>
        <td><input type="password" id="pwd" value="" /></td>
    </tr>
     

    <tr>
        <td> </td>
        <td><input type="button" id="btnLogin" class="layui-btn layui-bg-blue" value="登录" onclick="Login()" /></td>
    </tr>
</table>

<script>
    //就绪函数里初始化用户名跟密码
    $(function () {
        RemberUserNameAndPwd();
    });

    function RemberUserNameAndPwd(){ 
        var name = Cookies.get("username");//从cookie取用户名
        var pwd = Cookies.get("pwd");//从cookie 里取密码

        $("#name").val(name);
        $("#pwd").val(pwd);
    }

    function Login() {
        var name = $("#name").val();
        var pwd = $("#pwd").val(); 

        if (!name) {
            alert("用户名不能为空!"); return;
        }
        if (!pwd) {
            alert("密码不能为空!"); return;
        }

         

        $.ajax({

            url: 'https://localhost:7061/api/Member/Login?userName=' + name + '&passWord=' + pwd,
            type: 'get',
            contentType: 'application/json',
            success: function (res) {
                if (res == 1) {
                    alert("登录成功!");
                    Cookies.set("username", name, { expires: 7 });//在cookies里存了用户名  { expires: 7 }:7天过期
                    Cookies.set("pwd", pwd, { expires: 7 });//在cookies里存了用户名

                    location.href = '/member/ChengGong';

                } else if (res == -1) {
                    alert("用户名不存在!");
                } else {
                    alert("密码错误!");
                }
            }
        });

    }
</script>